<template >
<el-container>
  <el-header>
    <div>
      <img src="../assets/logo.png" alt="">
      <span>电商后台管理系统</span>
    </div>
      <el-button type = "info" @click = "logout">退出</el-button>
  </el-header>
  
  <el-container>
    <el-aside :width="isCollapse ? '64px':'200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <!-- 侧边栏菜单 -->
       <el-menu
      router
      :default-active=activePath
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF"
      :unique-opened="true"
      :collapse="isCollapse"
      :collapse-transition="false"
      >
      <!-- 一级菜单 -->
      <el-submenu :index=" item.id + '' " v-for="item in menuList" :key="item.id">
        <template slot="title">
          <!-- 图标 -->
          <i class="el-icon-location"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/' + subItem.path " v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path )">
          <!-- 二级菜单 -->
         <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data(){
    return{
      menuList:[],
      isCollapse:false,
      activePath:""
    }
  },
    methods: {
        logout(){
            window.sessionStorage.clear()
            this.$router.push("/login")
        },
        getMenuList(){
          this.$http.get("menus").then((res)=>{
            if(res.data.meta.status !== 200) return this.$message.console.error(res.meta.msg);
            this.menuList = res.data.data
          })
        },
        // 切换菜单折叠和展开
        toggleCollapse(){
          this.isCollapse = !this.isCollapse
        },
      // 刷新后保持链接的高亮状态
      saveNavState(activePath){
          window.sessionStorage.setItem("activePath",activePath)
        this.activePath=activePath
      },
    },   
    mounted(){
      // 获取导航菜单
      this.getMenuList()
    },
  created() {
    this.activePath =   window.sessionStorage.getItem("activePath")
  }
}
</script>

<style scoped lang="less">

.el-button{
  padding: 0;
  width: 60px;
  height: 40px;
  text-align: center;
  line-height: 40px;
}
.el-container{
  height: 100%;
}
.el-header{
  display: flex;
  justify-content: space-between;
  background-color: #373D41;
  align-items: center;
  color:#fff;
  font-size: 20px;
  img{
    height: 60px;
    margin-right: 15px;
  }
  div{
    display: flex;
    align-items: center;
  }
}
.el-aside{
  background-color: #333744;
}
.el-main{
  background-color: #eaedf1;
}
.el-menu{
  border-right: none;
}
.toggle-button{
  background-color: #4A5064;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  letter-spacing: .2em;
  cursor: pointer;
}
</style>